//
//  TWAllCategoriesController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/4/18.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit
import SVProgressHUD

class TWAllCategoriesController: UIViewController {

    fileprivate lazy var allCategoriesDataArray : [TWAllCategoriesModel] = [TWAllCategoriesModel]()
    lazy var collectionView : UICollectionView = {
        let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumInteritemSpacing = 2
        flowLayout.minimumLineSpacing = 2
        flowLayout.scrollDirection = .vertical
        let collectuonV = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
        collectuonV.backgroundColor = UIColor.white
        collectuonV.dataSource = self
        collectuonV.delegate = self
        collectuonV.contentInset = UIEdgeInsets(top: NavigationBarHeight, left: 0, bottom: 0, right: 0)
        collectuonV.showsVerticalScrollIndicator = true
        return collectuonV
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        SVProgressHUD.show()
        loadDatas()
        setupCollectionView()
        setupNav()
    }
}

// MARK:- UI界面
extension TWAllCategoriesController {
    fileprivate func setupCollectionView(){
        view.addSubview(collectionView)
        // 注册
        collectionView.register(UINib(nibName: "TWAllCategoriesSquareCardCell", bundle: nil), forCellWithReuseIdentifier: AllCategoriesSquareCardCell)
        collectionView.register(UINib(nibName: "TWAllCategoriesRectangleCardCell", bundle: nil), forCellWithReuseIdentifier: AllCategoriesRectangleCardCell)
    }
    fileprivate func setupNav(){
        let navView = UIView(frame: CGRect(x: 0, y: 0, width: TWScreenWidth, height: NavigationBarHeight))
        navView.backgroundColor = UIColor(red: 249 / 255.0, green: 249 / 255.0, blue: 249 / 255.0, alpha: 1.0)
        self.view.addSubview(navView)
        
        let dismissButton = UIButton(type: .custom)
        dismissButton.setImage(UIImage(named: "Action_close_44x44_"), for: .normal)
        dismissButton.addTarget(self, action: #selector(dismissButtonClick), for: .touchUpInside)
        navView.addSubview(dismissButton)
        dismissButton.snp.makeConstraints { (make) in
            make.left.bottom.equalTo(navView)
            make.height.width.equalTo(44)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = "全部分类"
        titleLabel.font = UIFont.init(name: "FZLTZCHJW--GB1-0", size: 18)
        titleLabel.sizeToFit()
        navView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(navView)
            make.centerY.equalTo(dismissButton)
        }
        
        let lineLabel = UILabel()
        lineLabel.backgroundColor = UIColor(red: 201 / 255.0, green: 201 / 255.0, blue: 201 / 255.0, alpha: 0.5)
        navView.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(navView)
            make.height.equalTo(0.5)
        }
    }
}

// MARK:- 请求数据
extension TWAllCategoriesController {
    fileprivate func loadDatas(){
        TWNetworkTool.shareInstance.loadAllCategoriesData(urlString: AllCategoriesUrlString) { (result, error) in
            if error != nil {
                SVProgressHUD.showError(withStatus: "加载失败！")
            }
            guard let result = result else {
                return
            }
            for data in result {
                let model = TWAllCategoriesModel(dict: data["data"] as! [String : AnyObject])
                self.allCategoriesDataArray.append(model)
            }
            SVProgressHUD.dismiss()
            self.collectionView.reloadData()
        }
    }
}

// MARK:- 监听点击
extension TWAllCategoriesController {
    @objc fileprivate func dismissButtonClick(){
        dismiss(animated: true, completion: nil)
    }
}

// MARK:- 代理
extension TWAllCategoriesController : UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allCategoriesDataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = allCategoriesDataArray[indexPath.item]
        let type = model.dataType!
        if type == "RectangleCard" {
            // 长方形
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AllCategoriesRectangleCardCell, for: indexPath) as! TWAllCategoriesRectangleCardCell
            cell.model = model
            return cell
        } else if type == "SquareCard" {
            // 正方形
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AllCategoriesSquareCardCell, for: indexPath) as! TWAllCategoriesSquareCardCell
            cell.model = model
            return cell
        } else {
            return UICollectionViewCell()
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = allCategoriesDataArray[indexPath.item]
        let type = model.dataType!
        if type == "RectangleCard" {
            // 长方形
            return CGSize(width: TWScreenWidth, height: TWScreenWidth / 2)
        } else if type == "SquareCard" {
            // 正方形
            return CGSize(width: TWScreenWidth / 2 - 2, height: TWScreenWidth / 2)
        } else {
            return CGSize(width: 0, height: 0)
        }
    }
}
